package com.example.administrator.systemfailreport.utils;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Created by Administrator on 2017/2/16.
 */
public class SwipeRefreshUtil {
    public static void onScroolSetSwipeRefreshEvent( final ListView lv ,final SwipeRefreshLayout srefl)
    {

        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                boolean isEnable = true;
                if(lv!=null){
                    ListAdapter adapter = lv.getAdapter();
                    if(adapter!=null&&adapter.getCount()>0){
                        boolean isFrist = lv.getFirstVisiblePosition()==0;
                        View fristChild = lv.getChildAt(0);
                        boolean isTop = true;
                        if (fristChild != null)
                            isTop = fristChild.getTop()-lv.getPaddingTop() == 0;
                        isEnable = isFrist && isTop;}
                    srefl.setEnabled(isEnable);
                }

            }
        });
    }
}
